import { Configuration } from "electron-builder";
import { Env, mainResolve } from "./utils";
import esbuild from "esbuild";
import configJson from "../app/package.json";
const external = [
  "electron",
  "nock",
  "aws-sdk",
  "mock-aws-s3",
  "@ghostery/adblocker-electron",
  "tldts-experimental",
];

function esBuildOptions(entry: string, platform: esbuild.Platform, target: string): esbuild.BuildOptions {
  return {
    bundle: true,
    sourcemap: process.env.NODE_ENV === "development",
    external,
    define: {
      ...Env.getInstance().loadDotEnvDefined(),
    },
    outdir: mainResolve("app/build/main"),
    loader: { ".png": "file" },
    minify: process.env.NODE_ENV === "production",
    entryPoints: [mainResolve(entry)],
    platform: platform,
    target: [target],
  };
}

export function browserOptions(entry: string): esbuild.BuildOptions {
  return esBuildOptions(entry, "browser", "chrome89");
}

export function nodeOptions(entry: string): esbuild.BuildOptions {
  return esBuildOptions(entry, "node", "node16.13");
}

export function getPackConfig(): Configuration {
  return {
    productName: "阿林播放器3.0",
    buildVersion: configJson.version,
    appId: process.env.APP_ID,
    copyright: process.env.APP_COPYRIGHT,
    artifactName:
      "${productName}-setup-${platform}-${arch}-${buildVersion}.${ext}",
    npmRebuild: true,
    // asar: false,
    directories: {
      output: "./release",
    },
    // publish:{
    //   provider: "generic",
    //   url: "https://gitee.com/yunyuehuang/player_publish/raw/master/"
    // },
    files: [ //这里的路径是基于./app的，其他地方的不是。app这个文件夹似乎是内置约定的。
      {
        from: "./build",
        to: "./",
      },
      "./package.json",
    ],
    extraResources: [
      // {
      //   from: "./app/plugin",
      //   to: "plugin",
      // },
      // {
      //   from: "./bin/${platform}/${arch}",
      //   to: "bin",
      // },
    ],
    win: {
      icon: "../assets/icon.ico",
      target: [
        // {  //setup安装包
        //   target: "nsis",
        //   arch: ["x64"],
        // },
        // "portable", // .exe的
      ],
    },
    portable: {
      artifactName:
        "${productName}-portable-${platform}-${arch}-${buildVersion}.${ext}",
    },
    dmg: {
      contents: [
        {
          x: 410,
          y: 150,
          type: "link",
          path: "/Applications",
        },
        {
          x: 130,
          y: 150,
          type: "file",
        },
      ],
    },
    mac: {
      icon: "../assets/icon.icns",
      target: [
        {
          target: "dmg",
          arch: [process.arch === "arm64" ? "arm64" : "x64"],
        },
      ],
    },
    linux: {
      category: "Utility",
      icon: "../assets/icon.icns",
      target: {
        target: "deb",
        arch: ["x64"],
      },
    },
    // nsis: {
    //   oneClick: false,
    //   allowElevation: true,
    //   allowToChangeInstallationDirectory: true,
    //   createDesktopShortcut: true,
    //   createStartMenuShortcut: true,
    // },
  };
}
